package com.sambo.inspection.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.lzy.okgo.model.HttpParams;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.bean.LoginResponseBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;
import com.sambo.inspection.util.SpUtil;

public class SetupActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String userName = SpUtil.getUserName();
                String password = SpUtil.getPassword();

                if (!TextUtils.isEmpty(userName)
                        && !TextUtils.isEmpty(password)) {
                    requestLogin(userName, password);
                } else {
                    forwardLogin();
                }
            }
        }, 2000);
    }

    private void requestLogin(final String userName, String password) {
        // 调用登录API
        HttpParams params = new HttpParams();
        params.put("username", userName);
        params.put("password", password);

        HttpClient.post(SetupActivity.this, Urls.API_SERVER_HOST + Urls.LOGIN, params,
                new HttpResponseHandler() {
                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        LoginResponseBean bean = JsonUtil.toObj(value.toString(), LoginResponseBean.class);
                        if (bean != null) {
                            GlobleBean.getInstance().setUserID(SpUtil.getUserID(userName));
                            GlobleBean.getInstance().setUserName(userName);
                            GlobleBean.getInstance().setAccessToken(bean.getAccess_token());
                            GlobleBean.getInstance().setNickName(SpUtil.getNickName(userName));
                            GlobleBean.getInstance().setPhone(SpUtil.getPhone(userName));
                            GlobleBean.getInstance().setLastMessageTS(SpUtil.getLastMessageTS(userName));

                            Intent intent = new Intent(SetupActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }

                    }

                    @Override
                    public void onInnovationFailure(String msg) {
                        super.onInnovationFailure(msg);
                        forwardLogin();
                    }

                }, new HttpClient.NetworkUnavailableListener() {
                    @Override
                    public void networkUnavailable() {
                        forwardLogin();
                    }
                });
    }

    private void forwardLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
